//
//  ChatImageCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/18.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ChatImageCell: ChatBaseCell {
    
    override func configure(with vm: ChatMsgViewModel, detailVm: AdvisoryDetailViewModel?) {
        super.configure(with: vm, detailVm: detailVm)
        
        if subviews.contains(chatImgView) {
            chatImgView.removeFromSuperview()
        }
        addSubview(chatImgView)
        
        chatImgView.kf.setImage(with: URL(string: vm.msgContent), placeholder: UIImage(named: "chat_image_placeholder"))
        
        setupUILayout(with: vm)
    }

    private lazy var chatImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.isUserInteractionEnabled = true
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(imgTap))
        imageView.addGestureRecognizer(tapGes)
        return imageView
    }()
    
    private func setupUILayout(with vm: ChatMsgViewModel) {
        // 重新布局
        avatarView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(kChatAvatarWidth)
            make.top.equalTo(sendTimeLabel.snp.bottom).offset(8)
        }
        chatImgView.snp.remakeConstraints { (make) in
            make.top.equalTo(avatarView)
            make.width.equalTo(100)
            make.height.equalTo(150)
        }
        bubbleView.snp.remakeConstraints { (make) in
            make.left.top.right.bottom.equalTo(chatImgView)
        }
        sendTimeLabel.snp.remakeConstraints { (make) in
            make.top.centerX.equalToSuperview()
        }
        errorView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        activityView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        
        switch vm.userType {
        case .patient:
            avatarView.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-kChatMargin)
            }
            chatImgView.snp.makeConstraints { (make) in
                make.right.equalTo(avatarView.snp.left).offset(-kChatMargin)
            }
            errorView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
            activityView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
        case .doctor:
            avatarView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(kChatMargin)
            }
            chatImgView.snp.makeConstraints { (make) in
                make.left.equalTo(avatarView.snp.right).offset(kChatMargin)
            }
        }
    }
    
    @objc private func imgTap() {
        guard let vm = viewModel else { return }
        delegate?.chatCell(self, didTappedImage: vm)
    }
}
